<template>
	<view class="page1">
		<view class="wrap s-flex-r-s-s">
			<view class="left">
				<view class="navList">
					<view class="navItem s-flex-c-c-c" :class="cateId1==tab.id?'active':''" v-for="(tab, i) in tabs"  :key="i" @click="tabClick1(i)">
						<text>{{tab.name}}</text>
					</view>
				</view>
			</view>
			<view class="right flex1 s-flex-c-s-s">
				<view class="tab-box s-flex-r-s-c" v-if="type2&&type2.length">
					<view class="flex1">
						<me-tabs :height="76" :tabsLineWidth="50" :activeClass="true" v-model="cate2Index" :tabs="type2" @change="typeChange2"></me-tabs>
					</view>
					<view class="priceSort s-flex-r-s-c" @click="handlePriceSort">
						<text class="text" v-if="priceUp">价格升序</text>
						<text class="text" v-else>价格降序</text>
						<view class="icon-box s-flex-c-c-c">
							<u-icon name="arrow-up-fill" :color="priceUp?'#39ACFF':'#D5D5D5'" size="20"></u-icon>
							<u-icon style="margin-top: -4rpx;" name="arrow-down-fill" :color="priceUp?'#D5D5D5':'#39ACFF'" size="20"></u-icon>
						</view>
					</view>
				</view>
				<view class="r-list flex1">
					<mescroll-uni ref="mescrollRef" @init="mescrollInit" height="100%" :down="downOption" :up="upOption" :fixed='false' top="0" bottom="0" @down="downCallback" @up="upCallback">
						<view class="list s-flex-r-b-s u-flex-wrap">
							<view class="item" v-for="(item,index) in list" :key="index" @click='goDetails(item.id)'>
								<!--  @click.stop="previewImg(item.mainCover)" -->
								<view class="img">
									<u-image class="img" width="228rpx" height="228rpx"  :src="item.mainCover" mode="aspectFit"></u-image>
								</view>
								<view class="name">{{item.goodsName}}</view>
								<view class="price">¥ {{item.price}}</view>
							</view>
						</view>
					</mescroll-uni>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js"
	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				cateId1:"",
				cateId:"",
				isAsc:"desc", //desc降序 asce 升序
				tabs:[],
				downOption:{
					auto:false
				},
				upOption:{
					auto:false
				},
				priceUp:true,
				cate2Index: 0,
				type2: [],
				keyword: "",
				list: []
			}
		},
		onLoad() {
			this.getCateList()
		},
		methods: {
			//预览图片
			previewImg(imgUrl) {
				uni.previewImage({
					urls: [imgUrl ]
				})
			},
			handlePriceSort(){
				this.priceUp= !this.priceUp
				this.isAsc=this.priceUp?'asc':'desc' //desc降序 asce 升序
				this.reload()
			},
			tabClick1(i){
				this.cateId1= this.tabs[i].id
				this.getCateList2()
			},
			typeChange2(i) {
				this.cateId= this.type2[i].id
				this.reload()
			},
			// 获取分类
			getCateList() {
				this.httpClient.post('agents/app/shopping/cateList', {parentId:0}).then(res => {
					this.tabs = res.data
					if(res.data&&res.data.length){
						this.cateId1= res.data[0].id
						this.getCateList2()
					}
				})
			},
			// 获取分类2
			getCateList2() {
				this.httpClient.post('agents/app/shopping/cateList', {parentId:this.cateId1}).then(res => {
					this.type2 = res.data
					if(res.data&&res.data.length){
						this.cateId= res.data[0].id
						this.reload()
					}else{
						this.list = []
					}
				})
			},
			reload() {
				this.list = []
				this.mescroll.resetUpScroll()
			},
			upCallback(page) {
				this.httpClient.post('agents/app/shopping/goodsList', {
					pageNum: page.num,
					pageSize: page.size,
					cateId: this.cateId,
					isAsc: this.isAsc,
					orderByColumn: "price",
				}).then(res => {
					this.mescroll.endByPage(res.rows.length, res.total);
					//this.mescroll.endSuccess(res.data.length);
					if (page.num == 1) this.list = [];
					this.list = this.list.concat(res.rows);
				}).catch(() => {
					this.mescroll.endErr();
				})
			},
			goDetails(id) {
				uni.navigateTo({
					url: 'shopDetails?id=' + id
				})
			}
		}
	}
</script>
<style lang="scss" scoped>
	/deep/ .mescroll-wxs-content{
		padding-bottom: 100rpx;
	}
	.page1 {
		/* #ifdef APP */
		height: 100%;
		/* #endif */
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		background: #f5f5f5;
		.wrap{
			height: 100%;
			overflow: hidden;
			.left{
				width: 228rpx;
				height: 100%;
				margin-right: 2rpx;
				overflow-y: scroll;
				.navItem{
					height: 74rpx;
					line-height: 74rpx;
				}
				.active{
					background: #fff;
				}
			}
			.right{
				height: 100%;
				width: 520rpx;
				overflow: hidden;
				background: #fff;
				.tab-box{
					height: 88rpx;
					.priceSort{
						padding-right: 16rpx;
						font-size: 20rpx;
						.icon-box{
							margin-left: 8rpx;
						}
					}
				}
			}
		}
	}

	.list {
		padding: 10rpx 0;
		box-sizing: border-box;
		.item {
			background: #fff;
			border-radius: 10rpx;
			padding: 16rpx;
			margin-bottom: 24rpx;
			box-sizing: border-box;
			.img {
				background-color: #E5E5E5;
			}
			.name{
				width: 228rpx;
				word-break:break-all; 				word-wrap:break-word;
			}
			.price {
				margin-top: 6rpx;
				color: #f14444;
				font-weight: bold;
			}
		}
	}
</style>